"""
cdn.models
Django models for servers

"""
import jackson.extjson.ext_json as simplejson

from django.db import models
from django.conf import settings

from jackson.chord import Server

class ServerField(models.TextField):
    def __init__(self, *args, **kwargs):
        # No special arguments here
        super(ServerField, self).__init__(*args, **kwargs)
    def to_python(self, value):
        # Handle the Server case
        print "ServerField.to_python: type of value =", value.__class__
        if isinstance(value, jackson.chord.Server):
            return value
        # The string case
        return simplejson.loads(value)
    def get_db_prep_value(self, value):
        return simplejson.dumps(value)

class ContentEntry(models.Model):
    #def __init__(self, key, *args, **kwargs):
        #self.key = key
        #self.file = None
        #self.url = ''
        #self.server = None
        #self.status = 'U'
        #self.md5_checksum = ''
        #self.urgency = 0
        #super(ContentEntry,self).__init__(key = key, url = url, server = server, status = status, md5_checksum = md5_checksum, urgency = urgency, *args, **kwargs)
    CONTENT_STATUS_CHOICES = (
        ('D', 'Delete'),
        ('I', 'Incoming'),
        ('H', 'Here'),
        ('O', 'Outgoing'),
        ('T', 'Transferring'),
        ('U', 'Uninitialized'),
    )
    key = models.CharField(max_length=40, primary_key=True, default='0000000000000000000000000000000000000000')
    #file = models.FileField(upload_to=settings.INSTANCE_NAME+'/', null=True)
    filename = models.CharField(max_length=255, default='')
    mimetype = models.CharField(max_length=255, default='')
    url = models.CharField(max_length=200, default='')
    server = ServerField(null=True, default=None)
    status = models.CharField(max_length=1, default='U') #, choices=CONTENT_STATUS_CHOICES)
    md5_checksum = models.CharField(max_length=40, default='')
    urgency = models.IntegerField(default=0)
    
class StateEntry(models.Model):
    def __init__(self, state, value):
        self.state = state
        self.value = value
    state = models.CharField(max_length=40, primary_key=True)
    value = models.TextField()
